﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.ContextMenu
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack)
      {
        return;
      }

      InitSpread(FpSpread1);
      InitSpreadStyles(FpSpread1.Sheets[0]);
      this.FpSpread1.UseClipboard = false;
    }

    private void InitSpread(FarPoint.Web.Spread.FpSpread spread)
    {
      using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb;Persist Security Info=True"))
      {
        using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT TOP 100 Products.ProductName as 产品, Products.UnitPrice as [单价], Products.UnitsOnOrder as [订单数量], Products.UnitsInStock as [库存] FROM Products", connection))
        {
          DataTable products = new DataTable("Products");
          adapter.Fill(products);
          FpSpread1.DataSource = products;
        }
      }

      FarPoint.Web.Spread.ContextMenu vpMenu = new FarPoint.Web.Spread.ContextMenu();
      vpMenu.Type = FarPoint.Web.Spread.ContextMenuType.Viewport;

      FarPoint.Web.Spread.MenuItem header = new FarPoint.Web.Spread.MenuItem("单元格菜单");
      header.Enabled = false;

      FarPoint.Web.Spread.MenuItem copyVpItem = new FarPoint.Web.Spread.MenuItem("复制");
      copyVpItem.ImageUrl = "~/explore/css/images/contextmenu/Copy.png";

      FarPoint.Web.Spread.MenuItem pasteVpItem = new FarPoint.Web.Spread.MenuItem("粘帖");
      pasteVpItem.ImageUrl = "~/explore/css/images/contextmenu/Paste.png";

      vpMenu.Items.AddRange(new FarPoint.Web.Spread.MenuItem[] {
          header,
          copyVpItem,
          pasteVpItem 
      });

      spread.EnableContextMenu = true;
      spread.ContextMenus.Add(vpMenu);

      spread.CssClass = "spreadStyle";
      spread.UseClipboard = false;
    }

    private void InitSpreadStyles(FarPoint.Web.Spread.SheetView sheet)
    {
      sheet.ColumnCount = 4;
      sheet.PageSize = 50;

      FarPoint.Web.Spread.ContextMenuStyle menuStyle = sheet.ContextMenuStyle;
      menuStyle.BackColor = Color.FromArgb(234, 236, 245);
      menuStyle.Seperator = new FarPoint.Web.Spread.Border();
      menuStyle.Seperator.BorderStyle = BorderStyle.Solid;
      menuStyle.ItemDisabledBackColor = Color.FromArgb(121, 153, 194);
      menuStyle.ItemDisabledForeColor = Color.White;


      sheet.Columns[0].Width = 200;
      sheet.Columns[1].Width = 80;
      sheet.Columns[2].Width = 100;
      sheet.Columns[3].Width = 80;
    }
  }
}